package com.prototype.prototype1;

public class TestSheep {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("tom" , 12 , "yello" , new Dog("边牧"));

        //clone返回的是一个obj，需要强转
        //使用原型模式，就算Sheep类增加了属性，下面的代码都不需要修改的
        Sheep sheep1 = (Sheep) sheep.clone();
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        Sheep sheep4 = (Sheep) sheep.clone();
        Sheep sheep5 = (Sheep) sheep.clone();
        // ····

        System.out.println(sheep.toString());
        System.out.println(sheep1.toString());
        System.out.println(sheep2.toString());
        System.out.println(sheep3.toString());
        System.out.println(sheep4.toString());
        System.out.println(sheep5.toString());

        //修改sheep的属性检查，克隆类是否会发生修改 ,基本类型是拷贝了一份自己使用，引用累心是
        System.out.println("-------- 修改源对象的属性 --------");
        sheep.setName("Jake");
        Dog master = sheep.getMaster();
        master.setName("哈士奇");
        sheep.setMaster(master);
        System.out.println(sheep);
        System.out.println(sheep1);

        System.out.println("------------- 修改克隆对象的属性，看源对象是否会变化 ------------------");
        sheep1.setName("Rose");
        Dog master1 = sheep1.getMaster();
        master1.setName("柯基");
        sheep1.setMaster(master1);
        System.out.println(sheep);
        System.out.println(sheep1);

        System.out.println("通过修改你会发现，如果源对象或者克隆对象的基本类型数据变了对其他克隆独享是没有影响的，" +
                "如果源对象或者克隆对象的引用类型属性变了，对其他克隆出来的对象都有影响，说明引用类型对象在克隆的时候属于是浅拷贝，拷贝的引用");

    }
}
